#include <stdio.h>
#include <string.h>

/* START:: code for queue */
#define QUEUE_SIZE 32
int out_queue_err = 0;

typedef struct {
	int data[QUEUE_SIZE];
	int inpos;
	int outpos;
} queue_t;

void init_queue(queue_t *q)
{
	memset(q->data, 0, sizeof(int) * QUEUE_SIZE);
	q->inpos = q->outpos = 0;
}

int in_queue(queue_t *q, int new)
{
	int pos = (q->inpos + 1) % QUEUE_SIZE; 
	if (pos == q->outpos)
		return -1;
	q->data[q->inpos] = new;
	q->inpos = pos;
	return 0;
}

int out_queue(queue_t *q) 
{
	int ret;

	if (q->outpos == q->inpos) {
		out_queue_err = -1;
		return -1;
	}

	ret = q->data[q->outpos];
	q->outpos = (q->outpos + 1) % QUEUE_SIZE;

	return ret;
}

int empty(queue_t *q)
{
	return q->inpos == q->outpos;
}
/* END:: code for queue */

int main()
{
	queue_t q[1];
	int i = 0, n = 10;

	init_queue(q);
	for (i = 0; i < n; i++)
		in_queue(q, i);

	while (!empty(q)) {
		int tmp = out_queue(q);
		printf("%d ", tmp);
	}
	printf("\n");

	for (i = 0; i < 32; i++)
		in_queue(q, i);

	while (!empty(q)) {
		int tmp = out_queue(q);
		printf("%d ", tmp);
	}
	printf("\n");

	for (i = 0; i < 32; i++)
		in_queue(q, i);

	while (!empty(q)) {
		int tmp = out_queue(q);
		printf("%d ", tmp);
	}
	printf("\n");

	return 0;
}


